package com.yllt4cloud.common.ddbase.result;

import com.yllt4cloud.common.ddbase.annotation.Descritpion;

/**
 * Created by Administrator on 2016/1/13.
 */
public class PageResult {
    @Descritpion(description = "当前页")
    private Integer pageNo;
    @Descritpion(description = "每页条数")
    private Integer pageSize;
    @Descritpion(description = "总记录数")
    private long totalCount;

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * 设置当前页的页号,序号从1开始,低于1时自动调整为1.
     */
    public void setPageNo(final int pageNo) {
        this.pageNo = pageNo;

        if (pageNo < 1) {
            this.pageNo = 1;
        }
    }

    /**
     * 根据记录在总结果集中的位置,计数出其所在页的页码
     * */
    public void setPageNoByFirst(int first) {
        setPageNo(first / this.pageSize + 1);
    }



    /**
     * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
     */
    public long getFirst() {
        return (this.pageNo - 1) * this.pageSize + 1;
    }

    /**
     * 根据pageNo和pageSize计算当前页最后一条记录在总结果集中的位置,序号从1开始.
     */
    public long getLast() {
        long last = getFirst() + this.pageSize - 1;
        return last > this.totalCount ? this.totalCount : last;
    }



    /**
     * 根据pageSize与totalCount计算总页数, 默认值为-1.
     */
    public long getTotalPages() {
        if (this.totalCount < 0) {
            return -1;
        }

        long count = this.totalCount / this.pageSize;
        if (this.totalCount % this.pageSize > 0) {
            count++;
        }
        return count;
    }

    /**
     * 是否还有下一页.
     */
    public boolean isHasNext() {
        return this.pageNo + 1 <= getTotalPages();
    }

    /**
     * 取得下页的页号, 序号从1开始. 当前页为尾页时仍返回尾页序号.
     */
    public int getNextPage() {
        if (isHasNext()) {
            return this.pageNo + 1;
        } else {
            return this.pageNo;
        }
    }

    /**
     * 是否还有上一页.
     */
    public boolean isHasPre() {
        return this.pageNo - 1 >= 1;
    }

    /**
     * 取得上页的页号, 序号从1开始. 当前页为首页时返回首页序号.
     */
    public int getPrePage() {
        if (isHasPre()) {
            return this.pageNo - 1;
        } else {
            return this.pageNo;
        }
    }
}
